package com.power.sifa.net.dao.impl;

import com.power.sifa.net.dao.Apl_formDao;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.*;

@Log
@Repository
public class Apl_formDaoImpl implements Apl_formDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;


    @Override
    public List<Map<String, Object>> queryListByApplyid(String table, String applyid) {
        String sql = "select * from " + table + " where applyid = ?";
        return this.jdbcTemplate.queryForList(sql, new Object[] { applyid });
    }

    @Override
    public List<Map<String, Object>> queryBatchList(String table, String relafield, List<String> ids) {
        StringBuffer sql = new StringBuffer("select * from " + table + " where " + relafield + " in (");
        List parsms = new ArrayList();
        for (String id : ids) {
            sql.append("?,");
            parsms.add(id);
        }
        sql = new StringBuffer(sql.toString().substring(0, sql.length() - 1));
        sql.append(")");
        List<Map<String, Object>> list = this.jdbcTemplate.queryForList(sql.toString(), parsms.toArray());
        List<Map<String, Object>> result = new ArrayList<>(10);
        for (Map<String, Object> v : list) {
            Map<String, Object> tempMap = new HashMap<>(16);
            for (Map.Entry<String, Object> entry : v.entrySet()) {
                tempMap.put(entry.getKey().toLowerCase(), entry.getValue());
            }
            result.add(tempMap);
        }
        return result;
    }

    @Override
    public List<Map<String, Object>> queryStdByIds(String table, String[] ids) {
        StringBuffer sql = new StringBuffer("select * from " + table + " where id in (");
        List parsms = new ArrayList();
        for (String id : ids) {
            sql.append("?,");
            parsms.add(Integer.valueOf(id));
        }
        sql = new StringBuffer(sql.toString().substring(0, sql.length() - 1));
        sql.append(")");
        List<Map<String, Object>> list = this.jdbcTemplate.queryForList(sql.toString(), parsms.toArray());
        List<Map<String, Object>> result = new ArrayList<>(10);
        for (Map<String, Object> v : list) {
            Map<String, Object> tempMap = new HashMap<>(16);
            for (Map.Entry<String, Object> entry : v.entrySet()) {
                tempMap.put(entry.getKey().toLowerCase(), entry.getValue());
            }
            result.add(tempMap);
        }
        return result;
    }
}
